package com.china08.yxyapi.api.schwebsite.index;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.entity.db.sws.SwsSettingSliders;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.db.SwsSettingSlidersRepository;
import com.china08.yxyapi.util.ThumborUtil;

/**
 * 学校网站首页:轮播图
 * 
 * @author Ertao.Fang
 *
 */
@CrossOrigin
@RestController("SwsIndexSliderController")
public class SwsIndexSliderController extends YxyApiBaseController {

    @Autowired
    private SwsSettingSlidersRepository swsSettingSlidersRepository;

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @RequestMapping(value = "/api_yw/schwebsite/index/slider", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String domainName = getDomainName(request);

        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains settingDomains = swsSettingDomainsRepository.findByDomainName(domainName);

        Assert.isTrue(null != settingDomains, "域名对象为空");

        String schoolId = settingDomains.getSchoolId();

        List<SwsSettingSliders> sliders = swsSettingSlidersRepository.findTop6BySchoolIdOrderByOrderNumDesc(schoolId);
        Assert.isTrue(null != sliders, "图片为空");

        List<RespModel> list = new ArrayList<RespModel>();
        for (SwsSettingSliders swsSettingSliders : sliders) {
            RespModel respModel = new RespModel();
            if (swsSettingSliders.getImg().startsWith("https")) {
                respModel.img = swsSettingSliders.getImg().replace("https:", "");
            } else {
                respModel.img = swsSettingSliders.getImg().replace("http:", "");
            }
            String imgFormat = swsSettingSliders.getImg().substring(swsSettingSliders.getImg().lastIndexOf(".") + 1);
            if (imgFormat.equals("gif")) {
                respModel.img_375_180 = respModel.img;
            } else {
                if (StringUtils.isNotBlank(swsSettingSliders.getImg())) {
                    respModel.img_375_180 = ThumborUtil.schoolWebSiteResize(swsSettingSliders.getImg(), 375, 180);
                } else {
                    respModel.img_375_180 = "";
                }
            }
            if (StringUtils.isNotBlank(swsSettingSliders.getImg())) {
                respModel.img_736_280 = ThumborUtil.schoolWebSiteResize(swsSettingSliders.getImg(), 736, 280);
            } else {
                respModel.img_736_280 = "";
            }
            respModel.url = swsSettingSliders.getUrl();
            respModel.id = swsSettingSliders.getId();
            list.add(respModel);
        }

        return list;

    }

    protected static class ReqModel extends YxyApiReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {

        public String id;

        public String img;

        public String img_375_180;

        public String img_736_280;

        public String url;

    }
}